package com.vq3.digitalizationsoftwareintegrates.Servlet.CheckCode;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.vq3.digitalizationsoftwareintegrates.Utils.GetYAML;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.QRCodeUtil;
import com.vq3.digitalizationsoftwareintegrates.Utils.PackagedDataUtil.Impl.PackagedDataImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**验证码的生成
 * @author y.h
 * @version 1.0.0
 * */

@RestController
@RequestMapping("/CC")
public class CheckCode extends PackagedDataImpl {


    final
    HttpServletResponse httpServletResponse;
    final
    HttpServletRequest httpServletRequest;

    public CheckCode(HttpServletResponse httpServletResponse, HttpServletRequest httpServletRequest) {
        this.httpServletResponse = httpServletResponse;
        this.httpServletRequest = httpServletRequest;
    }

    private static final String FileFormat=".png";
    private static final ThreadLocal<SimpleDateFormat> LOCALDATEFORMAT=ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));

    @GetMapping("/checkCode")
    public void getCode() throws IOException {
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(80,30,4,5);
        String code = circleCaptcha.getCode();
        HttpSession session  = httpServletRequest.getSession();
        session.setAttribute("CODE",code);
        System.out.println(code);
        circleCaptcha.write(httpServletResponse.getOutputStream());
    }
    @PostMapping("generate")
    public void generateV2(String content, HttpServletResponse servletResponse){


            try {
                final String fileName=LOCALDATEFORMAT.get().format(new Date());
                // TODO: 2023/1/31 演示的时候记得改路径  笔记本是H： 台式机是F：
                String rootPath = "F://Spot_check//TempImg//Img//";
                QRCodeUtil.createCodeToFile(content,new File(rootPath),fileName+FileFormat);
            }catch (Exception e){

            }



    }
}

